function filterWSDLFilesList(keyCode) {
	if (keyCode == '13') {
		$
				.ajax( {
					url : 'wsil',
					dataType : "xml",
					// beforeSend: function(xhr) {xhr.setRequestHeader("Accept",
					// "text/plain")},
					success : function(msg) {
						var guid = 0;
						$("#listOfFiles").html(
								"<div>List Of WSDL Files</div>\r\n");
						$(msg)
								.find('service')
								.each(
										function() {
											var name = $(this).find('name')
													.text();
											if (name.indexOf($("#filterText")
													.val()) != -1) {
												var div = jQuery("<div id=\"fileItem"
														+ guid
														+ "\" class=\"fileItem\"></div>\r\n");
												div
														.append("<a href=\"#\" onclick=\"showSourceFieldWSDL('fileItem"
																+ guid
																+ "')\">"
																+ name
																+ "</a>\r\n");
												div
														.append("<input type=\"button\" value=\"Delete\" style=\"float:right;\" onclick=\"deleteWSDLNode('fileItem"
																+ guid
																+ "')\" />\r\n");
												div
														.append("<input type=\"button\" value=\"Download\" style=\"float:right;\" onclick=\"downloadWSDL('fileItem"
																+ guid
																+ "')\" />\r\n");
												$("#listOfFiles").append(div);
												guid++;
											}
										});
					}
				});
	}
}

function loadWSDLFiles() {
	$
			.ajax( {
				url : 'wsil',
				dataType : "xml",
				success : function(msg) {
					var guid = 0;
					$("#listOfFiles").html("<div>List Of WSDL Files</div>\r\n");
					$(msg)
							.find('service')
							.each(
									function() {
										var name = $(this).find('name').text();
										var div = jQuery("<div id=\"fileItem"
												+ guid
												+ "\" class=\"fileItem\"></div>\r\n");
										div
												.append("<a href=\"#\" onclick=\"showSourceFieldWSDL('fileItem"
														+ guid
														+ "')\">"
														+ name
														+ "</a>\r\n");
										div
												.append("<input type=\"button\" value=\"Delete\" style=\"float:right;\" onclick=\"deleteWSDLNode('fileItem"
														+ guid + "')\" />\r\n");
										div
												.append("<input type=\"button\" value=\"Download\" style=\"float:right;\" onclick=\"downloadWSDL('fileItem"
														+ guid + "')\" />\r\n");
										$("#listOfFiles").append(div);
										guid++;
									});
				}
			});
}

function loadWSDLFilesSearch() {
	var s = "";
	if ($("input[name='where']:checked").val() == "porttype") {
		s = "type=portType&targetNamespace=" + $("#namespaceFile").val()
				+ "&name=" + $("#nameFile").val();
	} else {
		s = "type=service&targetNamespace=" + $("#namespaceFile").val()
				+ "&name=" + $("#fullnameFile").val();
	}
	$("#listOfFiles").html("<div>List Of WSDL Files</div>\r\n");
	$("#listOfFiles").show();
	$
			.ajax( {
				url : 'wsil',
				data : s,
				dataType : "xml",
				success : function(msg) {
					var guid = 0;
					$(msg)
							.find('service')
							.each(
									function() {
										var name = $(this).find('name').text();
										var div = jQuery("<div id=\"fileItem"
												+ guid
												+ "\" class=\"fileItem\"></div>\r\n");
										div
												.append("<a href=\"#\" onclick=\"showSourceFieldWSDL('fileItem"
														+ guid
														+ "')\">"
														+ name
														+ "</a>\r\n");
										div
												.append("<input type=\"button\" value=\"Delete\" style=\"float:right;\" onclick=\"deleteWSDLNode('fileItem"
														+ guid + "')\" />\r\n");
										div
												.append("<input type=\"button\" value=\"Download\" style=\"float:right;\" onclick=\"downloadWSDL('fileItem"
														+ guid + "')\" />\r\n");
										$("#listOfFiles").append(div);
										guid++;
									});
				}
			});
}

function searchChange(t) {
	if (t == 1) {
		$("#searchInputs")
				.html(
						"Name:<br /><input id=\"nameFile\" type=\"text\" style=\"width: 205px;\" /><br />Namespace:<br /><input id=\"namespaceFile\" type=\"text\" style=\"width: 205px;\" /><br />");
	} else {
		$("#searchInputs")
				.html(
						"Full Name:<br /><input id=\"fullnameFile\" type=\"text\" style=\"width: 205px;\" /><br />Namespace:<br /><input id=\"namespaceFile\" type=\"text\" style=\"width: 205px;\" /><br />");
	}
}

function showHome() {
	$("#underTopPanel").html("Home");
	$("#uploadFieldDialog").hide();
	$("#listOfFiles").hide();
	$("#helloDialog").show();
	$("#sourceField").hide();
	$("#editPanel").hide();
	$("#filterPanel").hide();
	$("#wsdlTreePanel").hide();
	$("#searchWithOptions").hide();
	setEditMode(false);
}

function addFile() {
	$("#underTopPanel").html(
			"<a href=\"#\" onclick=\"showHome()\">Home</a> &gt; WSDL Upload");
	$("#uploadFieldDialog").show();
	$("#listOfFiles").hide();
	$("#helloDialog").hide();
	$("#sourceField").hide();
	$("#editPanel").hide();
	$("#filterPanel").hide();
	$("#wsdlTreePanel").hide();
	$("#searchWithOptions").hide();
	setEditMode(false);
}

function showWSDLFilesList() {
	loadWSDLFiles();
	$("#underTopPanel").html(
			"<a href=\"#\" onclick=\"showHome()\">Home</a> &gt; WSDL");
	$("#uploadFieldDialog").hide();
	$("#listOfFiles").show();
	$("#helloDialog").hide();
	$("#sourceField").hide();
	$("#editPanel").hide();
	$("#filterPanel").show();
	$("#wsdlTreePanel").hide();
	$("#searchWithOptions").hide();
	setEditMode(false);
}

function showSourceFieldWSDL(divId) {
	$("#underTopPanel")
			.html(
					"<a href=\"#\" onclick=\"showHome()\">Home</a> &gt; <a href=\"#\" onclick=\"showWSDLFilesList()\">WSDL</a> &gt; "
							+ $('a:first', $("#" + divId)).text());
	$("#uploadFieldDialog").hide();
	$("#listOfFiles").hide();
	$("#helloDialog").hide();
	$("#sourceField").show();
	$("#editPanel").show();
	$("#filterPanel").hide();
	$("#wsdlTreePanel").hide();
	$("#searchWithOptions").hide();
	document.getElementById('downloadButton').onclick = function() {
		downloadWSDL(divId);
	};
	document.getElementById('deleteButton').onclick = function() {
		deleteWSDLNode(divId);
	};
	document.getElementById('editButton').onclick = function() {
		showEditMenu(divId);
	};
	loadWSDLText(divId);
	setEditMode(false);
}

function showWSDLTree() {
	$("#underTopPanel").html(
			"<a href=\"#\" onclick=\"showHome()\">Home</a> &gt; WSDL Tree");
	$("#uploadFieldDialog").hide();
	$("#listOfFiles").hide();
	$("#helloDialog").hide();
	$("#sourceField").hide();
	$("#editPanel").hide();
	$("#filterPanel").hide();
	$("#wsdlTreePanel").show();
	$("#searchWithOptions").hide();
	loadWSDLTreeData();
}

function loadWSDLTreeData() {
	$.tree.reference('wsdlTree').refresh();
	for ( var i = 0; i < 10; i++) {
		$.tree.reference('wsdlTree').create( {
			attributes : {
				id : 'porttypeWSDLTree' + i.toString(),
				rel : 'folder'
			},
			data : "portTypeName" + i.toString()
		}, "#nodesWSDL");
		$.tree.reference('wsdlTree').create( {
			attributes : {
				id : 'serviceWSDLTree' + i.toString(),
				rel : 'folder'
			},
			data : "ServiceName" + i.toString()
		}, "#porttypeWSDLTree" + i.toString());
		$.tree.reference('wsdlTree').create( {
			attributes : {
				id : 'policyWSDLTree' + i.toString(),
				rel : 'folder'
			},
			data : "policyName" + i.toString()
		}, "#serviceWSDLTree" + i.toString());
		$.tree.reference('wsdlTree').close_branch($('#porttypeWSDLTree' + i.toString()));
	}
}

function showSearchWithOptions() {
	$("#underTopPanel").html(
			"<a href=\"#\" onclick=\"showHome()\">Home</a> &gt; Search");
	$("#uploadFieldDialog").hide();
	$("#listOfFiles").hide();
	$("#helloDialog").hide();
	$("#sourceField").hide();
	$("#editPanel").hide();
	$("#filterPanel").hide();
	$("#wsdlTreePanel").hide();
	$("#searchWithOptions").show();
}

function xml2Str(xmlNode) {
	try {
		return (new XMLSerializer()).serializeToString(xmlNode);
	} catch (e) {
		try {
			return xmlNode.xml;
		} catch (e) {
			alert('Xmlserializer not supported');
		}
	}
	return false;
}

function loadWSDLText(divId) {
	$.ajax( {
		url : 'wsdl/' + $('a:first', $("#" + divId)).text(),
		success : function(msg) {
			$("#sourceArea").text(xml2Str(msg));
			refreshVisualTree();
		}
	});
}

function downloadWSDL(divId) {
	location.replace("jQuery-admin?download="
			+ $('a:first', $("#" + divId)).text());
}

function deleteWSDLNode(divId) {
	$("#sourceFieldDialog").dialog( {
		title : 'Confirmation: Delete WSDL File?',
		width : 250,
		height : 150,
		resizable : false,
		draggable : true,
		modal : true,
		position : 'center',
		buttons : {
			'Delete' : function() {
				$.ajax( {
					url : 'wsdl/' + $('a:first', $("#" + divId)).text(),
					type : 'DELETE',
					success : function(msg) {
						setEditMode(false);
						$("#" + divId).remove();
						showWSDLFilesList();
					}
				});
				$(this).dialog('close');
			},
			'Cancel' : function() {
				$(this).dialog('close');
			}
		},
		close : function() {
			$("#sourceFieldDialog").dialog("destroy");
		}
	});
}

function saveWSDL(divId) {
	$("#sourceFieldDialog").dialog(
			{
				title : 'Confirmation: Save WSDL File?',
				width : 250,
				height : 150,
				resizable : false,
				draggable : true,
				modal : true,
				position : 'center',
				buttons : {
					'Save' : function() {
						$.ajax( {
							url : "jQuery-admin",
							type : "POST",
							data : 'update='
									+ $('a:first', $("#" + divId)).text()
									+ '&text=' + $('#sourceArea').val(),
							success : function(msg) {
								if (msg == "ok") {
									setEditMode(false);
								} else {
									alert('Error Saving');
								}
							}
						});
						$(this).dialog('close');
					},
					'Cancel' : function() {
						$(this).dialog('close');
					}
				},
				close : function() {
					$("#sourceFieldDialog").dialog("destroy");
				}
			});
}

function setEditMode(m) {
	if (m == true) {
		$("#editButton").val("View");
		$("#sourceArea").css("color", "#000000");
		$("#sourceArea").removeAttr("readonly");
		$("#sourceArea").focus();
		editMode = true;
	} else {
		$("#editButton").val("Edit");
		$("#sourceArea").css("color", "#888888");
		$("#sourceArea").attr("readonly", "readonly");
		editMode = false;
	}
}

function showEditMenu(divId) {
	if (editMode == true) {
		saveWSDL(divId);
	} else {
		setEditMode(true);
	}
}

$(document).ready(function() {
	$("#tabs").tabs();
	$("#sourceFieldDialog").hide();
	initVisualTree();
	initWSDLTree();
});
function initVisualTree() {
	$("#visualTree").tree( {
		data : {
			type : "json",
			opts : {
				static : [ { attributes : {	id : "nodes",	rel : "root"},	state : "open",	data : "Definitions",
								children : [ { attributes : { id : "types", rel : "folder" }, data : "Types" },
								             { attributes : { id : "messages", rel : "folder" }, data : "Messages" },
								             { attributes : { id : "porttype", rel : "folder" }, data : "PortType" },
								             { attributes : { id : "services", rel : "folder" }, data : "Services" } ]
						} ]
			} 
		},
		types : {
			"default" : {
				deletable : true,
				createble : true,
				clickable: false,
				renameable : false,
				draggable : false,
			},
			"file" : {
				clickable: true,
				icon : {
					image : "js/themes/default/file.png"
				}
			}
		},
		ui : {
			theme_name : "default"
		},
		callback : {
			onselect : function(NODE, TREE_OBJ) {
				selectVisualTreeNode(NODE, TREE_OBJ);
			}
		}
	});
}

function initWSDLTree() {
	$("#wsdlTree").tree( {
		data : {
		type : "json",
		opts : {
			static : [ {
				attributes : {
					id : "nodesWSDL",
					rel : "root"
				},
				state : "open",
				data : "Root Node"
			} ]
		} },
		types : {
			"default" : {
				deletable : true,
				createble : true,
				renameable : false,
				draggable : false,
			},
			"file" : {
				icon : {
					image : "js/themes/default/file.png"
				}
			}
		},
		ui : {
			theme_name : "default"
		},
		callback : {
			onselect : function(NODE, TREE_OBJ) {
				// selectWSDLTreeNode(NODE, TREE_OBJ);
			}
		}
	});
}

function selectVisualTreeNode(NODE, TREE_OBJ) {
	if ($(NODE).parent().parent().attr('id').indexOf('porttype') == -1) {
		if (($(NODE).parent().parent().attr('id')) == 'types') {
			$('#selectedNodeInfo').html("");
		} else if (($(NODE).parent().parent().attr('id')) == 'messages') {
			xmlobject = parseXML($("#sourceArea").text());
			$('#selectedNodeInfo').html("");
			$(xmlobject)
					.find('[name=' + $('a:first', NODE).text() + ']')
					.find('part')
					.each(
							function() {
								$('#selectedNodeInfo')
										.append(
												"<span style=\"background-color: #EEE; border-top: thin solid #AAA; border-left: thin solid #AAA; border-right: thin solid #AAA; border-bottom: thin solid #FFF;\">message</span><br />");
								msgdiv = $("<div style=\"margin-bottom: 5px; border: thin solid #AAA;\"></div>");
								msgdiv.append("<b>name:</b> "
										+ $(this).attr('name') + "<br />");
								msgdiv.append("<b>element:</b> "
										+ $(this).attr('element') + "<br />");
								$('#selectedNodeInfo').append(msgdiv);
							});
		} else if (($(NODE).parent().parent().attr('id')) == 'services') {
			xmlobject = parseXML($("#sourceArea").text());
			$('#selectedNodeInfo').html("");
			$(xmlobject)
					.find('[name=' + $('a:first', NODE).text() + ']')
					.find('service')
					.each(
							function() {
								$('#selectedNodeInfo')
										.append(
												"<span style=\"background-color: #EEE; border-top: thin solid #AAA; border-left: thin solid #AAA; border-right: thin solid #AAA; border-bottom: thin solid #FFF;\">service</span><br />");
								msgdiv = $("<div style=\"margin-bottom: 5px; border: thin solid #AAA;\"></div>");
								msgdiv.append("<b>name:</b> "
										+ $(this).attr('name') + "<br />");
								$('#selectedNodeInfo').append(msgdiv);
								$(this)
										.find('port')
										.each(
												function() {
													$('#selectedNodeInfo')
															.append(
																	"<span style=\"background-color: #EEE; border-top: thin solid #AAA; border-left: thin solid #AAA; border-right: thin solid #AAA; border-bottom: thin solid #FFF;\">port</span><br />");
													msgdiv2 = $("<div style=\"margin-bottom: 5px; border: thin solid #AAA;\"></div>");
													msgdiv2
															.append("<b>name:</b> "
																	+ $(this)
																			.attr(
																					'name')
																	+ "<br />");
													msgdiv2
															.append("<b>binding:</b> "
																	+ $(this)
																			.attr(
																					'binding')
																	+ "<br />");
													$('#selectedNodeInfo')
															.append(msgdiv2);
													$(this)
															.find('address')
															.each(
																	function() {
																		$(
																				'#selectedNodeInfo')
																				.append(
																						"<span style=\"background-color: #EEE; border-top: thin solid #AAA; border-left: thin solid #AAA; border-right: thin solid #AAA; border-bottom: thin solid #FFF;\">address</span><br />");
																		msgdiv3 = $("<div style=\"margin-bottom: 5px; border: thin solid #AAA;\"></div>");
																		msgdiv3
																				.append("<b>location:</b> "
																						+ $(
																								this)
																								.attr(
																										'location')
																						+ "<br />");
																		$(
																				'#selectedNodeInfo')
																				.append(
																						msgdiv3);
																	});
												});

							});
		}
	} else {
		xmlobject = parseXML($("#sourceArea").text());
		$('#selectedNodeInfo').html("");
		$(xmlobject)
				.find('[name=' + $('a:first', NODE).text() + ']')
				.find('input')
				.each(
						function() {
							if ($(this).attr('message') != undefined) {
								$('#selectedNodeInfo')
										.append(
												"<span style=\"background-color: #EEE; border-top: thin solid #AAA; border-left: thin solid #AAA; border-right: thin solid #AAA; border-bottom: thin solid #FFF;\">input</span><br />");
								msgdiv = $("<div style=\"margin-bottom: 5px; border: thin solid #AAA;\"></div>");
								if ($(this).attr('name') != undefined) {
									msgdiv.append("<b>name:</b> "
											+ $(this).attr('name') + "<br />");
								}
								msgdiv.append("<b>message:</b> "
										+ $(this).attr('message') + "<br />");
								$('#selectedNodeInfo').append(msgdiv);
							}
						});
		$(xmlobject)
				.find('[name=' + $('a:first', NODE).text() + ']')
				.find('output')
				.each(
						function() {
							if ($(this).attr('message') != undefined) {
								$('#selectedNodeInfo')
										.append(
												"<span style=\"background-color: #EEE; border-top: thin solid #AAA; border-left: thin solid #AAA; border-right: thin solid #AAA; border-bottom: thin solid #FFF;\">output</span><br />");
								msgdiv = $("<div style=\"margin-bottom: 5px; border: thin solid #AAA;\"></div>");
								if ($(this).attr('name') != undefined) {
									msgdiv.append("<b>name:</b> "
											+ $(this).attr('name') + "<br />");
								}
								msgdiv.append("<b>message:</b> "
										+ $(this).attr('message') + "<br />");
								$('#selectedNodeInfo').append(msgdiv);
							}
						});
		$(xmlobject)
				.find('[name=' + $('a:first', NODE).text() + ']')
				.find('fault')
				.each(
						function() {
							if ($(this).attr('message') != undefined) {
								$('#selectedNodeInfo')
										.append(
												"<span style=\"background-color: #EEE; border-top: thin solid #AAA; border-left: thin solid #AAA; border-right: thin solid #AAA; border-bottom: thin solid #FFF;\">fault</span><br />");
								msgdiv = $("<div style=\"margin-bottom: 5px; border: thin solid #AAA;\"></div>");
								if ($(this).attr('name') != undefined) {
									msgdiv.append("<b>name:</b> "
											+ $(this).attr('name') + "<br />");
								}
								msgdiv.append("<b>message:</b> "
										+ $(this).attr('message') + "<br />");
								$('#selectedNodeInfo').append(msgdiv);
							}
						});
	}
	$('#selectedNodeName').text($('a:first', NODE).text());
}

function parseXML(txt) {
	try // Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(txt);
		return xmlDoc;
	} catch (e) {
		parser = new DOMParser();
		xmlDoc = parser.parseFromString(txt, "text/xml");
		return xmlDoc;
	}
	return xmlDoc;
}


function refreshVisualTree() {
	$.tree.reference("visualTree").refresh();
	xmlobject = parseXML($("#sourceArea").text());

	$(xmlobject).find("message").each(function() {
		name = $(this).attr("name");
		$.tree.reference("visualTree").create( {
			attributes : {
				rel : 'file'
			},
			data : {
				title : name,
			}
		}, "#messages");
	});

	$.tree.reference("visualTree").close_branch("#messages");

	i = 1;
	$(xmlobject).find("portType").each(
			function() {
				name = $(this).attr("name");
				$.tree.reference("visualTree").create( {
					attributes : {
						id : "porttype" + i.toString(),
						rel : "folder"
					},
					data : name
				}, "#porttype");
				$(this).find("operation").each(function() {
					n2 = $(this).attr("name");
					$.tree.reference("visualTree").create( {
						attributes : {
							rel : "file"
						},
						data : {
							title : n2,
							icon : "js/themes/default/file.png"
						}
					}, $("#porttype" + i.toString()));
				});
				$.tree.reference("visualTree").close_branch("#porttype" + i.toString());
				i++;
			});

	$.tree.reference("visualTree").close_branch("#porttype");

	 $(xmlobject).find("schema").each(function(){
		 $(this).children().each(function(){
			$.tree.reference("visualTree").create( { attributes : {	rel : 'file'}, data : { title : $(this).attr('name'), icon : "js/themes/default/file.png" }}, "#types");
		 });
	 });
	
	$.tree.reference("visualTree").close_branch("#types");

	$(xmlobject).find("service").each(function() {
		name = $(this).attr("name");
		$.tree.reference("visualTree").create( {
			attributes : {
				rel : "file"
			},
			data : {
				title : name,
				icon : "js/themes/default/file.png"
			}
		}, "#services");
	});

	$.tree.reference('visualTree').close_branch("#services");
}
